package cn;

import java.util.*;
import java.sql.*;
public class MysqlTest2 {
    public static void main(String args[]) {   
       String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
        Connection con;
        String sql;   
        int num;
       String name,sex;
       int age;
		
       try {
          Class.forName("oracle.jdbc.odbc.JdbcOdbcDriver");
       } catch(java.lang.ClassNotFoundException e) {
           System.err.print("ClassNotFoundException: ");
           System.err.println(e.getMessage());
       }
       
        try {	    
            con = DriverManager.getConnection(url, "system", "system");       
     
            Scanner in=new Scanner(System.in);
            System.out.print("\n输入学生性别：");               
             sex=in.nextLine();
           

           sql = " SELECT sno,sname,sage FROM STUDENT  where sex=?";         
            PreparedStatement stmt0=con.prepareStatement(sql);
             stmt0.setString(1,sex);//设置第一个“？”参数值为sex变量的值
             ResultSet rs = stmt0.executeQuery();
             
        	//检索student表中的对应sex性别的记录        
            System.out.println(" \n\n    "+sex+"学生信息表");  
            System.out.println("   ---------------------");  
        	System.out.println("   学号    姓名   年龄");  
            System.out.println("   ---------------------");
            while(rs.next()){
        	     num = rs.getInt("sno");
        	     name = rs.getString("sname");
        	     age = rs.getInt("sage");        		 
        	     System.out.println("    "+num+"    "+name+"   "+age);
        	}       
            System.out.println("   ---------------------");
        	//关闭连接
     	    stmt0.close();
        	con.close();
     	} catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        }          
    }
}

